package iut.acy.cut4u.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Planning {

        private static Planning instance;
        private  ArrayList<Employe> lesEmployes;
        private  ArrayList<HoraireSalon> lesHoraires;
        private  ArrayList<CongeAnnuel> lesCongesAnnuels;
        private Semaine semaineEnCours;
       
        public  static  Planning getInstance()
        {

                        return instance;
        }
                                       
        public  static  Planning getInstance(ArrayList<Employe> lesEmployes,
                        ArrayList<HoraireSalon> lesHoraires,
                        ArrayList<CongeAnnuel> lesCongesAnnuels,
                        Semaine semaineEnCours )
        {
                if (instance == null)
                        instance = new Planning(lesEmployes,
                                         lesHoraires,
                                         lesCongesAnnuels,
                                         semaineEnCours);
                return instance;
        }

        public Planning(ArrayList<Employe> lesEmployes,
                        ArrayList<HoraireSalon> lesHoraires,
                        ArrayList<CongeAnnuel> lesCongesAnnuels,
                        Semaine semaineEnCours) {
                super();
                this.lesEmployes = lesEmployes;
                this.lesHoraires = lesHoraires;
                this.lesCongesAnnuels = lesCongesAnnuels;
                this.semaineEnCours = semaineEnCours;
        }
       
        public boolean enregistreRendezVous (RendezVous rv)
        { if (this.estCompatiblePlanning(rv))
                        {if (this.semaineEnCours.getLesRendezVous().add(rv))
                                return true;
                        }
          return false;
        }
        public  boolean estCompatibleCongesAnnuels (RendezVous rv)
        {
                boolean compatible=true;
                for (CongeAnnuel f : lesCongesAnnuels)
                  {   if (( rv.getDate().compareTo(f.getDateDeb()) >=0)&&( rv.getDate().compareTo(f.getDateFin()) <=0))
                                   compatible=compatible&& false;
                  }
                  return compatible;
        }
       
        public  boolean estCompatibleHorairesOuvertures (RendezVous rv)
        {
                boolean compatible=true;
                int jour = rv.getDate().get(Calendar.DAY_OF_WEEK);
                if (jour == Calendar.SUNDAY)
                        jour =7;
                else
                        jour --;
                for (HoraireSalon h : lesHoraires)
                {
                if (h.getJour()==jour)
                        {
                        if  (rv.getHeureDebut().compareTo(new Horaire(12,0))<=0)
                                {
                                if (rv.getHeureDebut().compareTo(h.getHeureDebMatin())<0)
                                        compatible=compatible&false;
                                }
                        if  (rv.getHeureDebut().compareTo(new Horaire(12,0))>0)
                                {
                                if (rv.getHeureDebut().compareTo(h.getHeureDebAM())<0)
                                compatible=compatible&false;
                                }
                        }
                }
                return compatible;
        }
        public ArrayList<CongeAnnuel> getLesFermeturesAnnuelles() {
                return lesCongesAnnuels;
        }
        public ArrayList<HoraireSalon> getLesHorairesOuvertures() {
                return lesHoraires;
        }
        public void setLesFermeturesAnnuelles(
                        ArrayList<CongeAnnuel> lesFermeturesAnnuelles) {
                if (lesFermeturesAnnuelles !=null)
                        this.lesCongesAnnuels = lesFermeturesAnnuelles;
                else
                   this.lesCongesAnnuels = new ArrayList <CongeAnnuel>();
                }
        public void setLesHorairesOuvertures(
                        ArrayList<HoraireSalon> lesHorairesOuvertures) {
                if ( lesHorairesOuvertures !=null)
                           this.lesHoraires = lesHorairesOuvertures;
                else
                           this.lesHoraires = new ArrayList <HoraireSalon>();
        }
       
        public void changeSemaine ( int numeroSemaine){}
        public void semaineSuivante (){}
        public void semainePrecedente (){}
        public boolean estCompatibleAutresRendezVous (RendezVous rv)
        {
                boolean compatible=true;
                int jour = rv.getDate().get(Calendar.DAY_OF_WEEK);
               
                for (RendezVous r : this.semaineEnCours.getLesRendezVous())
                {
                        int jourLesRendezVous = r.getDate().get(Calendar.DAY_OF_WEEK);
                        if (((jour==jourLesRendezVous)&&(rv.getEmploye().getId()==r.getEmploye().getId()))||((jour==jourLesRendezVous)&&(rv.getClient().getId()==r.getClient().getId())))
                        {
                        if  ((rv.getHeureDebut().compareTo(r.getHeureDebut())>=0)&&(rv.getHeureDebut().compareTo(r.getHeureFin())<0))
                                compatible=compatible&false;
                        if  ((rv.getHeureFin().compareTo(r.getHeureDebut())>0)&&(rv.getHeureFin().compareTo(r.getHeureFin())<=0))
                                compatible=compatible&false;
                        }
                }
                return compatible;
        }
       
       
        public boolean estCompatiblePlanning (RendezVous rv)
        {
                return estCompatibleCongesAnnuels (rv)&&estCompatibleHorairesOuvertures (rv)&&estCompatibleAutresRendezVous (rv)&&rv.estCompatibleEmploye();
        }


}
